<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Message;
use App\Models\AdminUser;
use App\Models\Member;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class MessageController extends AdminController
{

    protected $options = [
        1 => '显示文本框',
        2 => '显示编辑器',
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {



        return Grid::make(new Message(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('type')->using([1 => '群发', 2 => '私发'])->label(['primary,danger']);
            // $grid->column('admin_id');
            // $grid->column('member_id');
            // $grid->column('status');
            $grid->column('content')->limit(30);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Message(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('type');
            $show->field('admin_id');
            $show->field('member_id');
            $show->field('status');
            $show->field('content');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        return Form::make(new Message(), function (Form $form) {




            $form->display('id');
            $form->text('title')->rules('required')->help('友情提示 请输入标题');

            // $form->radio('type')->options(['1' => '群发', '2' => '私发'])->default('1');
            $form->radio('type', '发布类型')
                ->options([
                    1 => '群发',
                    2 => '私发',
                ])->when(1, function (Form $form) {
                })->when(2, function (Form $form) {
                    $form->select('member_id')->options(function () {
                        $member = Member::class;
                        return $member::all()->pluck('nickname', 'id');
                    })->help('友情提示 请选择私发的用户');
                })->default(1);

            $form->select('admin_id')->options(function () {
                $adminUser = AdminUser::class;
                return $adminUser::all()->pluck('username', 'id');
            })->rules('required')->help('友情提示 请选择一个发布人');
            // $form->text('status');
            $form->textarea('content')->rules('required');

            $form->display('created_at');
            $form->display('updated_at');

            
            $form->saving(function (Form $form) {
                $type = $form->type;
                $member_id = $form->member_id;
                if ($type == 2 && !$member_id) {
                    return $form->response()->error('请选择用户接受人!');
                } 
               
            });



        });
    }
}
